#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"


	Date::Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	int Date::GetMonthDay(int year, int month)
	{
		int MonthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (month == 2&&((year%4 == 0&&year%100 != 0)||(year%400==0)))
		{
			return 29;
		}
		else
		{
			return MonthArray[month];
		}
	}

	void Date::print()
	{
		cout << _year << '/' << _month << '/' << _day << endl;
	}

	//d1 < d2
	bool  Date::operator<(Date& d)
	{
		if (_year<d._year)
		{
			return true;
		}
		else if (_year == d._year&&_month<d._month)
		{
			return true;
		}
		else if (_year == d._year&&_month==d._month
			&&_day<d._day)
		{
			return true;
		}

		return false;
	}

	//d1 == d2
	bool  Date::operator==(Date& d)
	{
		return _year == d._year&&_month == d._month
			&&_day == d._day;
	}


	//d1 <= d2
	bool  Date::operator<=(Date& d)
	{
		return *this < d || *this == d;
	}

	bool  Date::operator>=(Date& d)
	{
		return !(*this < d);
	}

	bool  Date::operator>(Date& d)
	{
		return !(*this <= d);
	}

	bool  Date::operator!=(Date& d)
	{
		return !(*this == d);
	}

	Date Date::operator+(int day)
	{
		Date tmp(*this);
		tmp._day += day;
		while (tmp._day>GetMonthDay(tmp._year,tmp._month))
		{
			tmp._day -= GetMonthDay(tmp._year, tmp._month);
			tmp._month++;
			if (tmp._month == 13)
			{
				tmp._month = 1;
				tmp._year++;
			}
		}
		return tmp;
	}

	Date& Date::operator +=(int day)
	{
		_day += day;
		while (_day>GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month == 13)
			{
				_month = 1;
				_year++;
			}
		}
		return *this;
	}

	Date& Date::operator=(Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		return *this;
	}

	ostream& operator<<(ostream& out, Date& d)
	{
		cout << d._year << "/" << d._month << "/" << d._day << endl;
		return out;
	}

	istream& operator>>(istream& in, Date& d)
	{
		cin >> d._year >> d._month >> d._day;
		return in;
	}

	Date& Date::operator -=(int day)
	{
		_day -= day;
		while (_day<=0)
		{
			_month--;
			if (_month == 0)
			{
				_month = 12;
				_year--;
			}
			_day += GetMonthDay(_year, _month);
		}
		return *this;
	}

	Date Date::operator -(int day)
	{
		Date tmp(*this);
		tmp._day -= day;
		while (tmp._day <= 0)
		{
			tmp._month--;
			if (tmp._month == 0)
			{
				tmp._month = 12;
				tmp._year--;
			}
			tmp._day += GetMonthDay(tmp._year, tmp._month);
		}
		return tmp;
	}

	int Date::operator -(Date& d)
	{
		int count = 0;
		int flag = 1;
		Date max(*this);
		Date min(d);
		if (d > *this)
		{
			flag = -1;
		}

		while (max != min)
		{
			if (flag == 1)
			{
				min += 1;
				count++;
			}
			else
			{
				max += 1;
				count++;
			}
		}
		
		return count*flag;
	}











